<template>
    <div class="main-container">
        <TableBody>
            <template #header>
                <TableHeader :show-filter="false">
                    <template #search-content>
                        <a-form layout="inline" :model="searchData">
                            <a-form-item label="所属公司">
                                <a-select popup-container="html"  v-model="searchData.firm" @change="searchGcChange">
                                    <a-option :key="0" value="">无</a-option>
                                    <a-option v-for="(gc, index) in gcList" :key="index">{{ gc.h_firm }}</a-option>
                                </a-select>
                            </a-form-item>
                            <a-form-item label="仓库名称">
                                <a-select popup-container="html"  v-model="searchData.warehouseName">
                                    <a-option :key="0" value="">无</a-option>
                                    <a-option v-for="(ck, index) in ckList" :key="index">{{ ck.name }}</a-option>
                                </a-select>
                            </a-form-item>
                            <a-form-item>
                                <a-button type="primary" @click="doRefresh">查询</a-button>
                            </a-form-item>
                        </a-form>

                    </template>
                </TableHeader>
            </template>
            <template #default>
                <a-table :bordered="false" :loading="tableLoading" :data="dataList" :pagination="false"
                         :row-key="rowKey">
                    <template #columns>
                        <a-table-column v-for="item of tableColumns" :key="item.key" :align="item.align"
                                        :title="(item.title as string)" :width="item.width"
                                        :data-index="(item.key as string)"
                                        :fixed="item.fixed">
                            <template v-if="item.key === 'index'" #cell="{ rowIndex }">
                                {{ rowIndex + 1 }}
                            </template>
                            <template v-else-if="item.key ==='checkType'" #cell="{ record }">
                                {{typetoStr(record.checkType)}}
                            </template>
                            <template v-else-if="item.key ==='keyInZt'" #cell="{ record }">
                                <a-tag v-if="record.keyInZt == 0" color="#f53f3f">未完成</a-tag>
                                <a-tag v-if="record.keyInZt == 1" color="#00b42a">已完成</a-tag>
                            </template>

                            <template v-else-if="item.key ==='actualZt'" #cell="{ record }">
                                <a-tag v-if="record.actualZt == 0" color="#f53f3f">未完成</a-tag>
                                <a-tag v-if="record.actualZt == 1" color="#00b42a">已完成</a-tag>
                            </template>

                            <template v-else-if="item.key === 'actions'" #cell="{ record }">
                                <a-space>
                                    <a-button v-if="record.actualZt===0" status="success" size="mini"
                                              @click="onUpdateItem(record)">RFID录入
                                    </a-button>
                                    <a-button v-if="record.actualZt===0" status="warning" size="mini"
                                              @click="onSubmitItem(record)">提交
                                    </a-button>
                                    <a-button v-if="record.actualZt===1&&record.balanceZt==0" status="warning" size="mini"
                                              @click="onRepealSubmit(record)">撤销提交
                                    </a-button>
                                </a-space>  <a-button status="warning" size="mini"
                                                      @click="detail(record)">明细
                            </a-button>
                            </template>
                        </a-table-column>
                    </template>
                </a-table>
            </template>

        </TableBody>
        <TableFooter ref="tableFooterRef" :pagination="pagination"/>

        <ModalDialog ref="qr3" title="RFID盘点" :width="1200" @confirm="editOk">
            <template #content>
                <a-space direction="vertical" fill>
                    <a-form ref="formRef2" :model="searchForm" size="small" :label-col-props="{ span: 4 }" layout="inline">
                        <a-form-item label="包标扫码/产品扫码">
                            <a-input size="mini" v-model="scanData" @press-enter="inputCode1" @input="sr()">
                            </a-input>
                        </a-form-item>
                        <a-form-item label="选择设备" v-if="sm.mode===1">
                            <a-select popup-container="html"  v-model="searchForm.wh" style="width:150px">
                                <a-option :value="0">请选择</a-option>
                                <a-option v-for="(item, index) in sbList" :key=index :value="item.id">{{
                                    item.equipName
                                    }}
                                </a-option>
                            </a-select>
                        </a-form-item>
                        <a-form-item label="感应类别" v-if="sm.mode===1">
                            <a-select popup-container="html"  v-model="searchForm.type">
                                <a-option>单个产品</a-option>
                                <a-option>产品包标</a-option>
                                <a-option>全部</a-option>
                            </a-select>
                        </a-form-item>
                        <a-form-item v-if="sm.mode===1">
                            <a-space >
                                <a-button type="outline" status="success" @click="testConnection">测试连接</a-button>
                                <!-- <a-button type="outline" status="success" @click="start">开始盘点</a-button>
                                 <a-button type="outline" status="success" @click="finishCheck">停止盘点</a-button>-->
                                <a-button type="outline" size="small" @click="start" :loading="flag">
                                    <template #icon>
                                        <icon-play-circle />
                                    </template>
                                    开始感应
                                </a-button>
                                <a-button type="outline" size="small" @click="finishCheck">
                                    <template #icon>
                                        <icon-record-stop />
                                    </template>
                                    停止感应
                                </a-button>
                            </a-space>
                        </a-form-item>
                    </a-form>
                    <a-table :columns="tagColumns" row-key="id" :data="tagsData" :pagination="false">
                        <template #columns>
                            <a-table-column v-for="item of tagColumns" :title="item.title" :key="item.key"
                                            :data-index="item.dataIndex">
                                <template v-if="item.key === 'action'" #cell="{ record }">
                                    <a-space>
                                        <a-popconfirm content="确认删除这个参数吗？" @ok="deleteTag(record)">
                                            <a-button status="danger" size="mini">删除</a-button>
                                        </a-popconfirm>
                                    </a-space>
                                </template>
                            </a-table-column>
                        </template>

                    </a-table>
                </a-space>
            </template>
        </ModalDialog>
        <ModalDialog ref="qr4" title="RFID盘点明细" :width="800">
            <template #content>
                <a-space direction="vertical" fill>
                    <a-table :columns="tagColumnsDetail" row-key="id" :data="tagsDataDetail" :pagination="false">
                        <template #columns>
                            <a-table-column v-for="item of tagColumnsDetail" :title="item.title" :key="item.key"
                                            :data-index="item.dataIndex">
                                <template v-if="item.key === 'action'" #cell="{ record }">
                                    <a-space>

                                    </a-space>
                                </template>
                            </a-table-column>
                        </template>

                    </a-table>
                </a-space>
            </template>
        </ModalDialog>
    </div>
</template>

<script lang="ts">
    import {post} from '@/api/http'
    import {  TableColumnData } from '@arco-design/web-vue'
    import {useRowKey, useTable, useTableColumn, usePagination} from '@/hooks/table'
    import {ModalDialogType} from '@/types/components'
    import {Message, Modal} from '@arco-design/web-vue'
    import {Form, Notification} from '@arco-design/web-vue'
    import {defineComponent, toRaw, onMounted, ref, reactive} from 'vue'
    import useUserStore from '@/store/modules/user'
    interface Equipment {
        equipAddress: string
        equipName: string
        equipPort: number
        firm: string
        id: number
        warehouseName: string
    }
    interface cplbEquipment {
        code: string
        id : number
    }
    export default defineComponent({
        name: 'StockCheckActual',
        setup: function () {
            const userStore = useUserStore()
            const cplbValue = ref('id');
            const fieldNames = {value: 'id', label: 'code'}
            // 设备在线列表
            const sbList = ref<Equipment[]>([])
            // 设备在线列表
            const cplbList = ref<cplbEquipment[]>([])
            const cplbData = ref('')
            onMounted(() => {
                post({
                    url: 'control/get',
                    data: {
                    }
                }).then(({ code, data }) => {
                    if (code === 200) {
                        sbList.value = data
                    } else {

                        Message.error({content:'设备列表获取失败',duration:5000})
                    }
                }).catch(() => {
                    Message.error({content:'网络错误',duration:5000})
                })
            })
            const form = reactive({
                IP: '',
                port: 0,
                name: ''
            })
            const searchForm = reactive({
                wh: 0,
                type: "单个产品",
                code: ""
            })
            interface PDScanModel {
                code: string
            }
            interface PDScanModelCp {
                code: string
            }
            interface PDScanModelPk {
                code: string
            }
            function scanDataToCardCp(str: string): PDScanModelCp {
                let pdDataCp: PDScanModelCp = {
                    code: ''
                }
                try {
                    let tempCp: PDScanModelCp = JSON.parse(str);
                    pdDataCp.code = tempCp.code
                    return pdDataCp
                } catch (error) {
                    Message.error({content:'数据解析失败',duration:5000})
                }
                throw new Error('数据解析失败')
            }
            function scanDataToCardPk(str: string): PDScanModelPk {
                let pdDataPk: PDScanModelPk = {
                    code: ''
                }
                try {
                    let tempCp: PDScanModelPk = JSON.parse(str);
                    pdDataPk.code = tempCp.code
                    return pdDataPk
                } catch (error) {
                    Message.error({content:'数据解析失败',duration:5000})
                }
                throw new Error('数据解析失败')
            }
            function scanDataToCard(str: string): PDScanModel {
                let pdData: PDScanModel = {
                    code: ''
                }
                try {
                    let temp: PDScanModel = JSON.parse(str);
                    pdData.code = temp.code
                    return pdData
                } catch (error) {
                    Message.error({content:'数据解析失败',duration:5000})
                }
                throw new Error('数据解析失败')
            }
            const inputCode1 = () => {

                if (scanData.value === '') {
                    return
                }
                let decode = scanDataToCard(scanData.value)
                let decodeCp = scanDataToCardCp(scanData.value)
                let decodePk = scanDataToCardPk(scanData.value)
                let map ={}

                map["decode"]=decode
                map["decodeCp"]=decodeCp
                map["decodePk"]=decodePk
                inputCode(map)
            }
            const inputCode = ( o:any) => {
                let decode = o["decode"]
                let decodeCp =o["decodeCp"]
                let decodePk = o["decodePk"]

                let arr = tagsData.value.filter((o: any) => {
                    return o.productInfoCode == decode.code||o.packLabelCode== decode.code
                })

                if (arr.length > 0) {
                    Message.warning({content:'请勿重复扫码！',duration:5000})
                    scanData.value = ''
                    scanData1.value = ''
                    return
                }
                let code1 = decode.code;
                let strings = code1.split("");
                var lb =strings[0]+strings[1]
                console.log(lb);
                if (lb==='PK'||lb==='PI'){
                    var arrData ={}
                    if (lb==='PK'){


                        arrData= {
                            "packLabelCode": decodePk.code
                        }
                    }
                    if (lb==='PI'){
                        arrData= {
                            "productInfoCode": decodeCp.code
                        }
                    }
                    if(lb!='PK'&&lb!='PI'){
                        Message.warning({ content: "请扫包标货产品二维码", duration: 5000 })
                        return;
                    }
                    post({
                        url: '/StockTurnOutCheck/getScanCode',
                        data: arrData
                    }).then(({ code, data }) => {
                        if (code==200){

                            data.forEach((s:any)=>{
                                tagsData.value.push(s)
                            })


                        }else {
                            Message.warning({ content: data, duration: 5000 })
                        }

                    }).finally(() => {
                        scanData.value = ''
                        scanData1.value = ''
                    })
                }

            }
            const pdlist = ref<any>([])
            const scanData = ref('')
            const scanData1 = ref('')
            const timeArr =ref<any>([]);
            function sr() {

                let length = searchForm.code.length;
                if (length % 2 !== 0) {
                    //求余数不为0是奇数
                    timeArr[0] = new Date().getTime();
                } else {
                    timeArr[1] = new Date().getTime();
                }
                //当输入第二位时判断两次输入的间隔，判断是否为手动输入，间隔过长清空值
                if (length > 1 && Math.abs(timeArr[1] - timeArr[0]) > 300) {

                    searchForm.code="";
                    Message.warning({ content: '请扫码', duration: 5000 })
                }
            }
            /*

                        var soCode: null =null;
                        var soCodestr: null =null;
                        const scanend = () => {

                            if (searchForm.code === '') {

                                return
                            }
                            try {
                                //出库单里应该有的东西
                                let scdata = JSON.parse(searchForm.code)
                                let soCode1 = scdata.code;
                                let split = soCode1.split("");
                                let str = split[0]+split[1];
                                let arr = tagsData.value.filter((o: any) => {
                                    return o.productInfoCode === soCode1
                                })

                                if (arr.length >0) {
                                    Message.warning({ content: '请勿重复扫码', duration: 5000 })
                                    return;
                                }
                                if (str=="PI"){

                                    soCodestr=soCode1
                                }
                                soCode=scdata
                                post({
                                    url: 'ProductInfo/listProductInfoDetail',
                                    data: {
                                        "code": scdata.code
                                    }
                                }).then(({ data, code }) => {
                                    if (code === 200) {
                                        if (tagsData.value.length>0&&str=="PI"){
                                            tagsData.value.push(data[0])
                                        }

                                    } else {
                                        Message.warning({
                                            content: data,
                                            duration: 5000
                                        })

                                    }
                                })
                            } catch (err) {

                            } finally {
                                searchForm.code = ''
                            }
                        }*/
            const table = useTable()
            const rowKey = useRowKey('id')
            const actionTitle = ref('添加盘点初始化')
            const tableColumns = useTableColumn([
                table.indexColumn,
                {
                    title: '盘点编号',
                    key: 'code',
                    dataIndex: 'code',
                    width: 150,
                },
                {
                    title: '所属工厂',
                    key: 'firm',
                    dataIndex: 'firm',
                    width: 150,

                },
                {
                    title: '盘点仓库',
                    key: 'warehouseName',
                    dataIndex: 'warehouseName',
                    width: 150,
                },
                {
                    title: '盘点类型',
                    key: 'checkType',
                    dataIndex: 'checkType',
                    width: 150,
                },
                {
                    title: '盘点时间',
                    key: 'checkTime',
                    dataIndex: 'checkTime',
                    width: 150,
                },

                {
                    title: '实际录入状态',
                    key: 'actualZt',
                    dataIndex: 'actualZt',
                    width: 150,
                },
                {
                    title: '实际录入时间',
                    key: 'actualTime',
                    dataIndex: 'actualTime',
                    width: 150,
                },
                {
                    title: '操作',
                    key: 'actions',
                    dataIndex: 'actions',
                    fixed: 'right',
                    width: 150,
                },
            ])
            const pagination = usePagination(doRefresh)
            // 搜索对象
            const searchData = reactive({
                "id": '',
                "code": '',
                "firm": '',
                "warehouseName": "",
                "checkType": "",
                "checkTime": ""
            })
            const tagsData = ref<{ productInfoCode: string, categorie: string, productName: string }[]>([])

            const tagColumns = [{
                title: '产品类别',
                dataIndex: "categorie",
                key: "categorie",
                width: 150,
            }, {
                title: '计算单位',
                dataIndex: 'countUnit',
                key: 'countUnit',
                width: 150,
            },
                {
                    title: '计量数量',
                    dataIndex: 'meterNum',
                    key: 'meterNum',
                    width: 150,
                }, {
                    title: '计量单位',
                    dataIndex: 'meterUnit',
                    key: 'meterUnit',
                    width: 150,
                }, {
                    title: '数量',
                    dataIndex: "num",
                    key: "num",
                    width: 150,
                }, {
                    title: '包标Code',
                    dataIndex: 'packLabelCode',
                    key: 'packLabelCode',
                    width: 150,
                }, {
                    title: '流水Code',
                    dataIndex: 'productInfoCode',
                    key: 'productInfoCode',
                    width: 150,
                }, {
                    title: '产品名称',
                    dataIndex: 'productName',
                    key: 'productName',
                    width: 150,
                },
                {
                    title: '规格',
                    dataIndex: 'spec',
                    key: 'spec',
                    width: 150,
                }]
            const tagsDataDetail = ref<{
                firm: string, productInfoCode: string, packLabelCode: string, shelveName: string, storageName: string,
                categorie: string, name: string, num: bigint, warehouseName: string, spec: string, meterUnit: string, countUnit: string, rkTime: string
            }[]>([])

            const tagColumnsDetail = [{
                title: '产品流水号',
                dataIndex: "productInfoCode",
                key: "productInfoCode",
                width: 150,
            }, {
                title: '产品属性',
                dataIndex: 'categorie',
                key: 'categorie',
                width: 150,
            }, {
                title: '产品名称',
                dataIndex: 'name',
                key: 'name',
                width: 150,
            },
                {
                    title: '规格',
                    dataIndex: 'spec',
                    key: 'spec',
                    width: 150,
                }, {
                    title: '计量单位',
                    dataIndex: 'meterUnit',
                    key: 'meterUnit',
                    width: 150,
                }, {
                    title: '计数单位',
                    dataIndex: 'countUnit',
                    key: 'countUnit',
                    width: 150,
                },
                {
                    title: '数量',
                    dataIndex: 'num',
                    key: 'num',
                    width: 150,
                }
            ]
            interface PD {
                name: string,
                spec: string,
                meterUnit: string,
                countUnit: string,
                code: string,
                categorie: string
            }
            const left = reactive<{ leftCols: TableColumnData[], leftData: PD[] }>({
                leftCols: [{
                    title: '产品编号',
                    dataIndex: 'code',
                    width: 150,
                }, {
                    title: '产品名称',
                    dataIndex: "name",
                    width: 150,
                }, {
                    title: '规格',
                    dataIndex: 'spec',
                    width: 150,
                }, {
                    title: '计量单位',
                    dataIndex: 'meterUnit',
                    width: 150,
                }, {
                    title: '计数单位',
                    dataIndex: 'countUnit',
                    width: 150,
                }, {
                    title: '操作',
                    dataIndex: 'actions',
                    fixed: 'right',
                    slotName: 'actions',
                    width: 150,
                }],
                leftData: []
            })

            // 请求表格数据的方法
            function doRefresh() {
                post({
                    url: 'StockCheckBegin/list',
                    data: {
                        page: pagination.page,
                        size: pagination.pageSize,
                        "zt": 1,
                        ...toRaw(searchData)
                    },
                })
                    .then(({data, code}) => {
                        if (code === 200) {
                            table.handleSuccess(data.records);
                            pagination.setTotalSize(data.total)
                        }

                    })
                    .catch(console.log)
            }

            // 修改 使用的对象
            const editData = reactive({
                "id": '',
                "code": '',
                "firm": '',
                "warehouseName": "",
                "checkType": "",
                "checkTime": ""
            })

            // RFID对象
            const qr3 = ref<ModalDialogType | null>(null);
            // RFID明细对象
            const qr4 = ref<ModalDialogType | null>(null);

            const options = ref([]);
            const formRef2 = ref<typeof Form>()
            const testConnection = () => {
                formRef2.value?.validate().then((err: any) => {
                    if (err) {
                        return
                    }
                    if (searchForm.wh === 0) {
                        Message.info('请先选择设备！')
                        return
                    }
                    var equipAddress="";
                    var equipPort=null;
                    sbList.value.filter((o) => {
                        if (o.id === searchForm.wh){
                            equipPort=o.equipPort
                            equipAddress=o.equipAddress
                        }
                    })
                    post({
                        url: '/rfidTest',
                        data: {
                            equipPort: equipPort,
                            equipAddress: equipAddress
                        }
                    }).then(({code}) => {
                        if (code === 200) {
                            Message.success('连接成功')
                        } else if (code === 10902) {
                            Message.error('连接失败')
                        } else if (code === 10901) {
                            Message.error('正在使用种')
                        } else if (code === 10101) {
                            Message.error('IP不存在')
                        }
                    }).catch(() => {
                        Message.error('连接失败')
                    })
                })

            }
            let curBatchNum = ''
            //批次号
            var pch = "";
            //开始感应
            let flag = ref(false)
            const start = () => {
                if (searchForm.wh === 0) {
                    Message.info('请先选择设备！')
                    return
                }
                let sb = sbList.value.filter((o) => {
                    return o.id === searchForm.wh
                })
                if (sb.length === 0) {
                    Message.warning({content:'当前机器已下线',duration:5000})
                    return
                }
                var type ="";
                if (searchForm.type === "单个产品"){
                    type="PI"
                }else if (searchForm.type === "产品包标"){
                    type="PK"
                }else {
                    type=""
                }
                let id = editData.id;
                post({
                    url: 'sendMessage/socket/pd',
                    data: {
                        "equipName": sb[0].equipName,
                        "equipAddress": sb[0].equipAddress,
                        "equipPort": sb[0].equipPort,
                        "type": type,
                        "productType": checkType,
                        "pdId":id
                    }
                }).then(({ data, code }) => {
                    if (code === 200) {
                        flag.value = true
                        curBatchNum = data;
                        setTimeout(fetchData, 2000);
                    } else {
                        Message.warning({content:data,duration:5000})
                    }
                })

            }

            const fetchData = (): void => {
                let sb = sbList.value.filter((o) => {
                    return o.id === searchForm.wh
                })
                try {
                    post({
                        url: "sendMessage/inventory",
                        data: {
                            "equipName": sb[0].equipName
                        },
                    }).then(({ code, data }: { code: number; data: any }) => {
                        if (code === 200) {
                            if (data.equipState === 0) {
                                setTimeout(fetchData, 2000);
                            }

                            if (Array.isArray(data.list)) {
                                data.list.forEach((o: any) => {
                                    tagsData.value.push( o);

                                });
                            }

                            //  left.leftData = data.concat(left.leftData);
                        }else {
                            flag.value = false
                        }
                    });
                } catch (err) {
                    Message.error({content:'拉取数据失败，已停止读取！',duration:5000})
                }
            };
            const formRef = ref<typeof Form>()
            const finishCheck = () => {
                formRef2.value?.validate().then((err: any) => {
                    if (err) {
                        return
                    }
                    if (searchForm.wh === 0) {
                        Message.info('请先选择设备！')
                        return
                    }
                    var equipName="";
                    sbList.value.filter((o) => {
                        if (o.id === searchForm.wh){
                            equipName=o.equipName
                        }
                    })
                    post({
                        url: '/rfidClose',
                        data: {
                            rfidName: equipName
                        }
                    }).then(({code}) => {
                        if (code === 200) {
                            Message.success('关闭成功')
                        } else if (code === 10902) {
                            Message.error('关闭失败')
                        } else if (code === 10901) {
                            Message.error('正在使用种')
                        }
                    }).catch(() => {
                        Message.error('关闭失败')
                    }).finally(() => {
                        flag.value=false
                    });

                })
            }

            // 修改 使用的对象
            const rfidData = reactive({
                "equipName": '',
                "equipAddress": '',
                "equipPort": '',
                "type": "",
                "productType": "",
                gcList: ["PK", "PI"],
                ckList: ['AA', 'BB']
            })
            var checkType=""
            function onUpdateItem(item: any) {
                if (item.lockZt==0){
                    Message.warning({ content: "未锁仓不能盘点", duration: 5000 })
                    return
                }
                editData.id = item.id
                rfidData.equipName = item.equipName
                rfidData.equipAddress = item.equipAddress
                rfidData.equipPort = item.equipPort
                rfidData.type = item.type
                checkType=item.checkType
                rfidData.productType = item.productType
                actionTitle.value = 'RFID盘点'
                searchForm.wh= 0
                tagsData.value.length = 0
                qr3.value?.toggle()

            }

            function xgState( item: any) {
                post({
                    url: 'StockCheckBegin/update',
                    data: {
                        id: item.id,
                        actualZt: item.actualZt
                    }
                }).then(({code,data}) => {
                    if (code === 200) {
                        doRefresh()
                    } else if (code === 10902) {
                        Message.error('连接失败')
                    } else if (code === 10901) {
                        Message.error('正在使用种')
                    }else if (code ===10302){
                        Message.warning({ content: data, duration: 5000 })
                    }
                })
            }

            //明细
            function detail(item: any) {

                editData.id = item.id
                rfidData.equipName = item.equipName
                rfidData.equipAddress = item.equipAddress
                rfidData.equipPort = item.equipPort
                rfidData.type = item.type
                rfidData.productType = item.productType
                qr4.value?.toggle()
                post({
                    url: 'StockCheckActual/getList',
                    data: {
                        "stockCheckBeginid": item.id
                    }
                }).then(({code, data}) => {
                    if (code === 200) {
                        tagsDataDetail.value = data
                    } else {
                        Message.success('失败')
                    }
                }).catch(() => {
                    Message.error({content:'网络错误',duration:5000})
                }).finally(() => {
                    doRefresh()
                })
            }

            function onSubmitItem(item: any) {


                post({
                    url: 'StockCheckActual/getList',
                    data: {
                        "stockCheckBeginid": item.id
                    }
                }).then(({code, data}) => {
                    if (code === 200) {
                        if (data.length==0){
                            Message.error({
                                content: '请先RFID录入',
                                duration: 5000
                            })
                            return
                        }else {
                            xgState({actualZt:1,id:item.id});
                        }
                    } else {
                        Message.success('失败')
                    }
                }).catch(() => {
                    Message.error({content:'网络错误',duration:5000})
                }).finally(() => {
                    doRefresh()
                })

            }
            //撤销提交
            function onRepealSubmit(item: any) {
                let balanceZt = item.balanceZt;
                if (balanceZt==1){
                    Message.info('已经平仓不能撤回');
                    return
                }
                xgState({actualZt:0,id:item.id});
            }
            function onDeleteItem(data: any) {
                Modal.confirm({
                    title: '提示',
                    content: '是否要删除此仓库？',
                    cancelText: '取消',
                    okText: '删除',
                    onOk: () => {
                        post({
                            url: 'StockCheckBegin/delete',
                            data: [data.id]
                        }).then(({code}) => {
                            if (code === 200) {
                                Message.success('删除成功')
                            } else {
                                Message.success('删除失败')
                            }
                        }).catch(() => {
                            Message.success('网络错误')
                        }).finally(() => {
                            doRefresh()
                        })
                    },
                })
            }

            const gcList = ref<{ warehouseName: string, id: number }[]>([])
            const ckList = ref<{ warehouseName: string, id: number }[]>([])
            const opt = ref([])
            // 加载
            onMounted(() => {

                post({
                    url: 'ProductCategoryCode/get'
                }).then(({ code, data }) => {
                    if (code === 200) {
                        opt.value = data
                        doRefresh()
                    }
                }).catch(() => {
                    Message.error({
                        content: '网络错误',
                        duration: 5000
                    })
                })
                doRefresh()
                post({
                    url: 'firm/getfrim',
                    data: {
                        "firm": "",
                        "bumen": "",
                        "zu": "",
                        "ziwei": "",
                        "ren": ""
                    }

                }).then((resp) => {
                    gcList.value = resp.data
                })
                post({
                    url: '/ProductCategoryCode/get',
                    data: {
                    }

                }).then((resp) => {
                    cplbList.value = resp.data
                })
            })

            const searchGcChange = () => {
                post({
                    url: 'warehouse/get',
                    data: {
                        isStop: 1,
                        firm: searchData.firm
                    }
                }).then(({data, code}) => {
                    if (code === 200) {

                        ckList.value = data
                    } else {
                        Message.warning({content:'获取数据失败！',duration:5000})
                    }
                })
                    .catch(() => {
                        Message.warning({content:'获取数据失败！',duration:5000})
                    })
            }
            const otherList = reactive<any>({
                cplxList: []
            })
            const editGcChange = () => {
                post({
                    url: 'warehouse/get',
                    data: {
                        isStop: 1,
                        firm: editData.firm
                    }
                }).then(({data, code}) => {
                    if (code === 200) {

                        ckList.value = data
                    } else {
                        Message.warning({content:'获取数据失败！',duration:5000})
                    }
                })
                    .catch(() => {
                        Message.warning({content:'获取数据失败！',duration:5000})
                    })
            }

            // 仓库状态修改事件
            const stateChange = (id: number, e: any) => {
                post({
                    url: 'StockCheckBegin/update',
                    data: {
                        id: id,
                        isStop: e ? '1' : '0'
                    }
                }).then(({code,data}) => {
                    if (code === 200) {
                        Message.success('修改成功')
                    } else {
                        Message.warning({ content: data, duration: 5000 })
                    }
                })
            }
            const editOk = () => {

                let value = tagsData.value;
                if (value.length==0){
                    Message.warning({ content: "请录入产品", duration: 5000 })
                }
                var arr =[];
                for (var ls in value){
                    let valueElement = value[ls];
                    for (var code in valueElement){
                        if (code==='productInfoCode'){
                            arr.push(valueElement[code])
                        }
                    }

                }

                post({
                    url: 'StockCheckActual/save?stockCheckBeginid=' + editData.id,
                    data:arr
                }).then(({code}) => {
                    if (code === 200) {
                        Notification.success('盘点成功')
                        qr3.value?.close();
                    } else if (code === 10902) {
                        Notification.error('盘点失败')
                    } else if (code === 10901) {
                        Notification.error('正在使用种')
                    }
                }).catch(() => {
                    Notification.error('连接失败')
                })
            }
            const typetoStr = (t: string): string => {
                if (!t || t == undefined) {
                    return ''
                } else {
                    let code = t.split(',')
                    let names = code.map((o) => {
                        let op = opt.value.filter((c: any) => {
                            return c.code === o
                        })

                        if (op.length === 0) {
                            return ''
                        } else {
                            return (op[0] as any).categorie || ''
                        }

                    })


                    return names.join(',')
                }
            }

            const sm =reactive({
                mode:userStore.systemMode
            })
            userStore.getMode().then(()=>{
                sm.mode = userStore.systemMode
            })
            return {
                sm,
                scanData,
                scanData1,
                sr,
                flag,
                opt,
                typetoStr,
                cplbData,
                cplbValue,
                fieldNames,
                cplbList,
                xgState,
                onSubmitItem,
                searchGcChange,
                editGcChange,
                editOk,
                gcList,
                ckList,
                pagination,
                stateChange,
                searchData,
                rowKey,
                editData,
                tableColumns,
                actionTitle,
                ...table,
                onDeleteItem,
                onUpdateItem,
                doRefresh,
                qr3,
                qr4,
                detail,
                form,
                testConnection,
                tagColumns,
                tagsData,
                start,
                finishCheck,
                tagsDataDetail,
                tagColumnsDetail,
                formRef,
                formRef2,
                searchForm,
                sbList,
                onRepealSubmit,
                otherList,
                options,
                inputCode1
            }
        },
    })
</script>
